//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.SystemClass;
import com.ruoyi.live.base.domain.User;
import com.ruoyi.live.base.mapper.SystemClassMapper;
import com.ruoyi.live.base.mapper.UserMapper;
import com.ruoyi.live.tool.commons.ICommons;
import com.ruoyi.live.tool.commons.IConvert;
import com.ruoyi.live.tool.commons.IMD5;
import com.ruoyi.live.tool.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;

@Service
public class UserService {
    private static final Logger log = LoggerFactory.getLogger(UserService.class);
    @Autowired
    UserMapper dao;
    @Autowired
    CacheService cacheService;
    @Autowired
    UserMapper userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    SystemClassMapper classDao;
    @Autowired
    DeployService deployService;

    public UserService() {
    }

    public void addUser(User entity) throws Exception {
        try {
            entity.setUserCreateUserId(SessionInfo.getUserId());
            entity.setUserPasswordMd5(IMD5.getMD5(entity.getPassword()));
            entity.setUserLoginNameMd5(IMD5.getMD5(entity.getUserLoginName()));
            entity.setUserHeaderImg("pic_tea.png");
            this.dao.insert(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("新增User失败,编号UserService-addUser-1");
            throw new Exception("新增失败,编号UserService-addUser-1");
        }
    }

    public void updateUser(User entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新User失败,编号UserService-updateUser-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号UserService-updateUser-1");
        }
    }

    public User getUser(Long id) {
        return (User)this.dao.selectById(id);
    }

    public int doRePass(String[] ids) throws Exception {
        if (ids.length == 0) {
            throw new Exception("请选择要删除的数据,编号UserService-deleteUser-1");
        } else {
            Long pass = IMD5.getMD5(this.cacheService.getParam("SYSTEM_DEFAULT_PASSWORD"));

            try {
                int count = 0;

                for(int i = 0; i < ids.length; ++i) {
                    if (!ICommons.isNullOrEmpty(ids[i])) {
                        count += this.dao.updateById((new User()).setUserPasswordMd5(pass).setId(IConvert.string2long(ids[i])));
                    }
                }

                return count;
            } catch (Exception var5) {
                var5.printStackTrace();
                log.error("删除User失败,编号UserService-deleteUser-2,ids:{}", ids.toString());
                throw new Exception("删除的数据失败,编号UserService-deleteUser-2");
            }
        }
    }

    public int deleteUser(String[] ids, int state) throws Exception {
        if (ids.length == 0) {
            throw new Exception("请选择要删除的数据,编号UserService-deleteUser-1");
        } else {
            try {
                int count = 0;

                for(int i = 0; i < ids.length; ++i) {
                    if (!ICommons.isNullOrEmpty(ids[i])) {
                        count += this.dao.updateById((new User()).setUserState(state).setId(IConvert.string2long(ids[i])));
                    }
                }

                return count;
            } catch (Exception var5) {
                var5.printStackTrace();
                log.error("删除User失败,编号UserService-deleteUser-2,ids:{}", ids.toString());
                throw new Exception("删除的数据失败,编号UserService-deleteUser-2");
            }
        }
    }

    public List<User> query(User entity) throws Exception {
        if (entity.getUserType() == null) {
            throw new Exception("查询失败");
        } else if (entity.getUserType() == 3 && entity.getClassId() == null) {
            throw new Exception("查询失败");
        } else {
            QueryWrapper<User> wrapper = (QueryWrapper)(((QueryWrapper)(((new QueryWrapper()).eq(!ICommons.isNullOrEmpty(entity.getClassId()), "class_id", entity.getClassId())).eq(!ICommons.isNullOrEmpty(entity.getImportId()), "import_id", entity.getImportId())).eq("user_type", entity.getUserType())).like(!ICommons.isNullOrEmpty(entity.getUserRealName()), "user_real_name", entity.getUserRealName())).like(!ICommons.isNullOrEmpty(entity.getUserLoginName()), "user_login_name", entity.getUserLoginName());
            List<User> users = this.dao.selectList(wrapper);
            Iterator var4 = users.iterator();

            while(var4.hasNext()) {
                User user = (User)var4.next();
                if (4 == user.getUserType() && !ICommons.isNullOrEmpty(user.getUserSchool())) {
                    user.setExpertClassName(((SystemClass)this.classDao.selectById(user.getUserSchool())).getClassName());
                }
            }

            return users;
        }
    }

    public String insertBatchUser(String start, Integer noStart, Integer noStop, Long classId, Integer userDuty) {
        Long password = IMD5.getMD5(this.cacheService.getParam("SYSTEM_DEFAULT_PASSWORD"));
        int success = 0;
        int error = 0;

        for(Integer i = noStart; i <= noStop; i = i + 1) {
            try {
                String userLoginName = start + i;
                this.userDao.insert((new User()).setClassId(classId).setUserPasswordMd5(password).setUserCreateUserId(SessionInfo.getUserId()).setUserCreateTime(System.currentTimeMillis()).setUserLoginName(userLoginName).setUserLoginNameMd5(IMD5.getMD5(userLoginName)).setUserRealName(userLoginName).setUserState(1).setUserType(3).setUserDuty(userDuty + "").setUserHeaderImg("pic_tea.png"));
                ++success;
            } catch (Exception var12) {
                var12.printStackTrace();
                ++error;
            }
        }

        return (success == 0 ? "" : success + "条记录插入成功") + (error == 0 ? "" : error + "条记录插入失败");
    }

    public void initSystem(String password, Integer type, HttpServletRequest request, HttpServletResponse response) throws Exception {
        User user = (User)this.userDao.selectById(1L);
        if (user.getUserPasswordMd5().equals(IMD5.getMD5(password))) {
            this.deployService.backup(request, response);
            this.jdbcTemplate.batchUpdate(new String[]{"delete from live_barrage_channel;", "delete from live_exam_expert;", "delete from live_exam_expert_detail;", "delete from live_exam_judge;", "delete from live_exam_objective_score;", "delete from live_exam_objective_score_detail;", "delete from live_exam_user;", "delete from live_task_user;", "delete from user_log;", "delete from live_task_user_channel;", "delete from live_task_user_detail;", "delete from live_task_user_put;", "delete from task_user_record;", "delete from task_user_learn_log;", "delete from task_user;"});
            if (2 == type) {
                this.jdbcTemplate.update("delete from user where user_type in (3,4)");
            }

        } else {
            throw new Exception("");
        }
    }

    public Message updatePsdSave(String userpass, String userpassnew) {
        User user = SessionInfo.getUser();
        if (user.getUserPasswordMd5().equals(IMD5.getMD5(userpass))) {
            this.dao.updateById((new User()).setId(user.getId()).setUserPasswordMd5(IMD5.getMD5(userpassnew)));
            return new Message(true, "修改成功");
        } else {
            return new Message(false, "登录密码错误");
        }
    }
}
